//
//  TTTabBar.m
//  Tint
//
//  Created by Kevin Wu on 4/13/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTTabBar.h"


@implementation TTTabBar



#pragma mark - Accessors

@synthesize delegate = _delegate;


@synthesize tabs = _tabs;

- (void)setTabs:(NSArray *)tabs {
    [_containerView removeAllSubviews];
    
    TTRetain(tabs, _tabs);
    for (int i=0; i<[_tabs count]; ++i) {
        TTTab *tab = [_tabs objectAtIndex:i];
        tab.tabbar = self;
        [_containerView addSubview:tab];
    }
    
    [self setNeedsLayout];
}



@synthesize customTabSize = _customTabSize;

- (void)setCustomTabSize:(BOOL)customTabSize {
    _customTabSize = customTabSize;
    [self setNeedsLayout];
}


@synthesize tabAlignment = _tabAlignment;

- (void)setTabAlignment:(TTTabBarTabAlignment)tabAlignment {
    _tabAlignment = tabAlignment;
    [self setNeedsLayout];
}


@synthesize edgeInsets = _edgeInsets;

- (void)setEdgeInsets:(UIEdgeInsets)edgeInsets {
    _edgeInsets = edgeInsets;
    [self setNeedsLayout];
}


@synthesize spacing = _spacing;

- (void)setSpacing:(CGFloat)spacing {
    _spacing = spacing;
    [self setNeedsLayout];
}



- (UIImage *)backgroundImage {
    return _backgroundImageView.image;
}

- (void)setBackgroundImage:(UIImage *)backgroundImage {
    _backgroundImageView.image = backgroundImage;
}






#pragma mark - Memory

- (id)initWithTabs:(NSArray *)tabs {
    self = [self init];
    if (self) {
        self.tabs = tabs;
    }
    return self;
}

- (id)init {
    self = [super init];
    if (self) {
        
        _delegate = nil;
        self.tabs = nil;
        
        _customTabSize = NO;
        _tabAlignment = TTTabBarTabAlignmentCenter;
        _edgeInsets = UIEdgeInsetsZero;
        _spacing = 0.0;
        
        _selectedTab = nil;
        
        _backgroundImageView = [[UIImageView alloc] init];
        _backgroundImageView.backgroundColor = [UIColor clearColor];
        [self addSubview:_backgroundImageView];
        
        _containerView = [[UIScrollView alloc] init];
        _containerView.delegate = self;
        _containerView.showsVerticalScrollIndicator = NO;
        _containerView.showsHorizontalScrollIndicator = NO;
        _containerView.bounces = NO;
        _containerView.backgroundColor = [UIColor clearColor];
        [self addSubview:_containerView];
    }
    return self;
}

- (void)dealloc {
    _delegate = nil;
    TTRelease(_tabs);
    
    TTRelease(_backgroundImageView);
    TTRelease(_containerView);
    [super dealloc];
}



#pragma mark - UIView

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [_backgroundImageView setFrameIfNeeded:self.bounds];
    
    [_containerView setFrameIfNeeded:UIEdgeInsetsInsetRect(self.bounds, _edgeInsets)];
    [_containerView makeContentSizeToViewSizeIfNeeded];
    
    [self layoutTabs];
}


#pragma mark - Public

- (void)update {
    TTTab *selectedTab = [self selectedTab];
    for (TTTab *tab in _tabs) {
        if (tab == selectedTab) {
            tab.state = UIControlStateSelected;
        } else {
            tab.state = UIControlStateNormal;
        }
    }
}

- (void)updateByTab:(TTTab *)tab {
    [self updateByTabAtIndex:[_tabs indexOfObjectIdenticalTo:tab]];
}

- (void)updateByTabAtIndex:(NSUInteger)index {
    TTTab *currentTab = [_tabs objectOrNilAtIndex:index];
    TTTab *selectedTab = [self selectedTab];
    if (currentTab != selectedTab) {
        [self updateCurrentTab:currentTab selectedTab:selectedTab];
    }
}

- (void)updateCurrentTab:(TTTab *)currentTab selectedTab:(TTTab *)selectedTab {
    if (currentTab.state == UIControlStateNormal) {
        // Do nothing
    } else if (currentTab.state == UIControlStateHighlighted) {
        selectedTab.state = UIControlStateNormal;
        _selectedTab = currentTab;
        [self didSelectTabAtIndex:[self selectedIndex]];
    } else if (currentTab.state == UIControlStateSelected) {
        // Do nothing
    }
}

- (void)selectTab:(TTTab *)tab {
    [self selectTabAtIndex:[_tabs indexOfObjectIdenticalTo:tab]];
}

- (void)selectTabAtIndex:(NSUInteger)index {
    TTTab *currentTab = [_tabs objectOrNilAtIndex:index];
    TTTab *selectedTab = [self selectedTab];
    if (currentTab != selectedTab) {
        selectedTab.state = UIControlStateNormal;
        currentTab.state = UIControlStateSelected;
        _selectedTab = currentTab;
        [self didSelectTabAtIndex:[self selectedIndex]];
    }
}


- (void)layoutTabs {
    if (!TTIsArrayWithItems(_tabs)) return;
    
    NSUInteger tabsCount = [_tabs count];
    
    if (_customTabSize) {
        
        CGFloat originX = 0.0;
        for (int i=0; i<tabsCount; ++i) {
            TTTab *tab = [_tabs objectAtIndex:i];
            CGSize idealSize = [tab idealSize];
            
            CGFloat originY = 0.0;
            if (_tabAlignment == TTTabBarTabAlignmentCenter) {
                originY = (_containerView.contentSize.height - idealSize.height) / 2.0;
            } else if (_tabAlignment == TTTabBarTabAlignmentBottom) {
                originY = _containerView.contentSize.height - idealSize.height;
            } else {
                originY = 0.0;
            }
            
            CGRect frame = TTRectMake(originX, originY, idealSize.width, idealSize.height);
            [tab setFrameIfNeeded:frame];
            
            originX += idealSize.width;
            originX += _spacing;
        }
        
        [_containerView setContentSizeIfNeeded:TTSizeMake(originX-_spacing, _containerView.contentSize.height)];
        [_containerView makeContentSizeToViewSizeIfNeeded];
        
    } else {
        
        CGFloat tabWidth = (_containerView.contentSize.width - (tabsCount-1)*_spacing) / tabsCount;
        CGFloat tabHeight = _containerView.contentSize.height;
        
        for (int i=0; i<tabsCount; ++i) {
            TTTab *tab = [_tabs objectAtIndex:i];
            CGRect frame = TTRectMake(i*(tabWidth+_spacing), 0.0, tabWidth, tabHeight);
            [tab setFrameIfNeeded:frame];
        }
        
    }
}


- (TTTab *)selectedTab {
    if ([self selectedIndex] != NSNotFound) {
        return _selectedTab;
    }
    return nil;
}

- (NSUInteger)selectedIndex {
    return [_tabs indexOfObjectIdenticalTo:_selectedTab];
}


- (void)didSelectTabAtIndex:(NSUInteger)index {
    [(NSObject *)_delegate runSelector:@selector(tabBar:didSelectTabAtIndex:) 
                            withObject:self 
                            withObject:(id)index];
}

@end
